package com.bnyy.adapter.adapter;

import android.content.Context;
import android.media.MediaPlayer;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mcxtzhang.swipemenulib.SwipeMenuLayout;

import java.util.ArrayList;

public abstract class BaseNormalListAdapter_<T> extends RecyclerView.Adapter<BaseNormalListAdapter_.Wapper> {
    private ArrayList<T> datas = new ArrayList<>();

    public Context mContext;
    public LayoutInflater inflater;
    private boolean noMore = false;
    private static final int NO_MORE = -10000;

    public View getFooterView() {
        TextView textView = new TextView(mContext);
        textView.setText("没有更多数据了");
        textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        textView.setPadding(10, 10, 10, 10);
        textView.setGravity(Gravity.CENTER);
        return textView;
    }

    public View getHeaderView() {
        return null;
    }

    @NonNull
    @Override
    public Wapper onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == NO_MORE) {
            return new Wapper<>(new ViewHolder(getFooterView(), ViewHolder.FOOTER));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull Wapper wapper, final int position) {
        if (wapper.vh instanceof BaseNormalListAdapter_.ViewHolder) {

        } else {
            if (onItemViewClickListener != null) {
                wapper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onItemViewClickListener.onClick(view, getData(position), position);
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        if (noMore) {
            return datas.size() + 1;
        }
        return datas.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (noMore && position == getItemCount() - 1) {
            return NO_MORE;
        }
        return super.getItemViewType(position);
    }

    public BaseNormalListAdapter_(Context context) {
        this.mContext = context;
        inflater = LayoutInflater.from(context);
    }

    public BaseNormalListAdapter_(Context context, OnClickListener<T> onItemViewClickListener) {
        this(context);
        setOnItemViewClickListener(onItemViewClickListener);
    }

    public void refresh(ArrayList<? extends T> datas) {
        noMore = false;
        this.datas.clear();
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    public void loadMore(ArrayList<? extends T> datas) {
        this.datas.addAll(datas);
        notifyItemRangeChanged(this.datas.size() - datas.size(), this.datas.size());
    }

    public void loadMoreFinish(ArrayList<? extends T> datas) {
        noMore = true;
        this.datas.addAll(datas);
        notifyItemRangeChanged(this.datas.size() - datas.size(), this.datas.size());
    }

    public void remove(int position) {
        datas.remove(position);
        notifyItemRemoved(position);
        notifyDataSetChanged();
    }

    public ArrayList<T> getDatas() {
        return datas;
    }

    public T getData(int index) {
        return datas.get(index);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public static final int HEADER = 0;
        public static final int FOOTER = 1;
        public View root;
        public int type;

        public ViewHolder(@NonNull View itemView, int type) {
            super(itemView);
            root = itemView;
            this.type = type;
        }
    }

    public static class Wapper<VH extends RecyclerView.ViewHolder> extends RecyclerView.ViewHolder {
        public View root;
        public VH vh;

        private Wapper(VH vh) {
            super(vh.itemView);
            this.vh = vh;
            root = vh.itemView;
        }
    }

    private OnClickListener<T> onItemViewClickListener;

    public void setOnItemViewClickListener(OnClickListener<T> onItemViewClickListener) {
        this.onItemViewClickListener = onItemViewClickListener;
    }


    public interface OnClickListener<T> {
        void onClick(View view, T t, int position);
    }
}

